/****************************************************
          File Name:52-4-server.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sun 21 Sep 2014 04:28:28 PM CST
*****************************************************/

#include"mq.h"
#include<signal.h>
#include<sys/wait.h>
#define o_flag (O_CREAT|O_RDWR)
#define O_ERR ((mqd_t)-1)

void dealing(int sig)
{
    if(mq_unlink(SERVER)==-1|| mq_unlink(CLIENT1)==-1
        || mq_unlink(CLIENT2)==-1 ||mq_unlink(CLIENT3)==-1)
        err("remove mq");
    exit(EXIT_SUCCESS);
}

int main(int argc, char *argv[])
{
    mqd_t server, client1, client2, client3;
    struct mq_attr attr;
    struct sigaction act;
    char *name_1, *name_2;
	
    if((server=mq_open(SERVER,o_flag,MODE,NULL))==O_ERR)
        err("mq_open server");
    if((client1=mq_open(CLIENT1,o_flag,MODE,NULL))==O_ERR)
        err("mq_open client1");
    if((client2=mq_open(CLIENT2,o_flag,MODE,NULL))==O_ERR)
        err("mq_open client2");
    if((client3=mq_open(CLIENT3,o_flag,MODE,NULL))==O_ERR)
        err("mq_open client3");

    if(mq_getattr(server,&attr)==-1)
        err("getattr");

    name_1=malloc(attr.mq_msgsize);
    if(name_1==NULL)
        err("malloc");
    name_2=malloc(attr.mq_msgsize);
    if(name_2==NULL)
        err("malloc");

    if(sigemptyset(&act.sa_mask)==-1)
        err("sigemptyset");
    act.sa_flags=0;
    act.sa_handler=dealing;

    if(sigaction(SIGINT,&act,NULL)==-1)
		err("sigaction");

	for(;;)
		pause();
    exit(EXIT_SUCCESS);
}
